package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkStationType;
import com.sgl.gk.service.GkStationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 工位类型的controller
 **/
@RestController
@RequestMapping("/gk/stationtype")
public class GkStationTypeController extends BaseController {

    @Autowired
    GkStationTypeService gkStationTypeService;

    /**
     * 按条件分页查询工位类型信息
     * @param gkStationType  查询条件
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(GkStationType gkStationType){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkStationType> list = gkStationTypeService.list(gkStationType);
        return getDataTable(list);
    }

    /**
     * 添加工位类型信息
     * @param gkStationType
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "工位类型",businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody GkStationType gkStationType){
        if (gkStationType.getName()==null||gkStationType.getName()==""||StringUtils.length(gkStationType.getName())>100){
            return AjaxResult.error("工位类型名称不能为空或者长度超过100个字符");
        }
        if (gkStationType.getCode()==null||gkStationType.getCode()==""||StringUtils.length(gkStationType.getCode())>100){
            return AjaxResult.error("工位类型编号不能为空或者长度不能超过100个字符");
        }
        gkStationType.setName(gkStationType.getName().trim());
        gkStationType.setCode(gkStationType.getCode().trim());
        return gkStationTypeService.add(gkStationType);
    }

    /**
     * 修改工位类型信息
     * @param gkStationType
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    @Log(title = "工位类型",businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody GkStationType gkStationType){
        if (gkStationType.getName()==null||gkStationType.getName()==""||StringUtils.length(gkStationType.getName())>100){
            return AjaxResult.error("工位类型名称不能为空或者长度超过100个字符");
        }
        if (gkStationType.getCode()==null||gkStationType.getCode()==""||StringUtils.length(gkStationType.getCode())>100){
            return AjaxResult.error("工位类型编号不能为空或者长度不能超过100个字符");
        }
        gkStationType.setName(gkStationType.getName().trim());
        gkStationType.setCode(gkStationType.getCode().trim());
        return gkStationTypeService.edit(gkStationType);
    }

    /**
     * 删除工位类型信息
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @ResponseBody
    @Log(title = "工位类型",businessType = BusinessType.DELETE)
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkStationTypeService.removeByIds(ids);
    }


}
